const { reject } = require("lodash");
const mysql = require("mysql2");

const options = {
    host:"127.0.0.1",//主机名
    port:"3306",
    user:"root",
    password:"12345678",
    database:"taro_express_mysql"
}

//创建数据库连接
const connection = mysql.createConnection(options);
//建立连接
connection.connect(err=>{
    if(err){
        console.log(err);
        return;
    }
    console.log("数据库连接成功");
})

//执行mysql查询命令
connection.query("select * from teacher_tab",(err,res,fields)=>{
    console.log("res:",res)
    console.log("err:",err)
})

const sqlQuery = (strSql) =>{
    return new Promise((resolve,reject)=>{
        connection.query(strSql,(err,res)=>{
            if(err){
                reject(err);
            }
            resolve(res);
        })
    })
}

module.exports = sqlQuery;
